<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }
        
        div {
            text-align: center;
            margin: 100px auto 0;
            height: 40px;
        }

        button {
            box-sizing: border-box;
            width: 98px;
            border: 2px solid gray;
            font-size: 20px;
            text-align: center;
            height: 40px;
        }
        main {
            margin: auto;
            border: 2px solid gainsboro;
            width: 300px;
            height: 500px;
        }
    </style>
</head>

<body>
    <div>
        <button>首页</button>
        <button>服务</button>
        <button>个人</button>
    </div>
    <main></main>
</body>

</html>
<script>
    let route =[
        {path:'/home',render:home_page},
        {path:'/service',render:service_page},
        {path:'/personal',render:personal_page},
    ]
    var bnns = [...document.querySelectorAll('button')]
    // console.log(bnns);
    bnns.forEach((item,index)=>{
        item.onclick = function(e){
            e.preventDefault()
            //调用history.pushState() 添加一个新的历史记录( 同时实现路由地址的切换 )
            history.pushState({},'',route[index].path)
            route[index].render()
        }
    })
    function home_page(){
        document.querySelector('main').innerHTML= '我是首页'
    }
    function service_page(){
        document.querySelector('main').innerHTML= '我是服务'
    }
    function personal_page(){
        document.querySelector('main').innerHTML= '个人中心'
    }
</script>